﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace JunionRobotTaskManager.Utility
{
    public class HttpHelper
    {
        public static string RequestPost(string url, string data, string ContentType="application/json")
        {
            return RequestPost(url, Encoding.GetEncoding("UTF-8").GetBytes(data), ContentType);
        }

        public static string RequestPost(string url, byte[] data, string ContentType)
        {
            if (string.IsNullOrEmpty(url))
            {
                return string.Empty;
            }
            string result = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.ContentType = ContentType;// "application/json";
            req.Method = "POST";
            req.ContentLength = data.Length;
            req.Timeout = 3000;
            try
            {
                using (Stream requestStream = req.GetRequestStream())
                {
                    requestStream.Write(data, 0, data.Length);
                }
                using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                {
                    using (Stream stream = resp.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (req != null)
                    req.Abort();

            }
            return result;
        }
        public static string RequestGet(string url, string data, string ContentType)
        {
            if (string.IsNullOrEmpty(url))
            {
                return string.Empty;
            }
            string result = string.Empty;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url + (data == "" ? "" : "?") + data);
            req.Method = "Get";
            req.ContentType = ContentType;
            req.Timeout = 3000;
            try
            {
                using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                {
                    using (Stream stream = resp.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (req != null)
                    req.Abort();

            }
            return result;
        }
        public static string Get(string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return null;
            }
            string result = null;
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "Get";
            req.ContentType = "application/json";
            req.Timeout = 10000;
            try
            {
                using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                {
                    using (Stream stream = resp.GetResponseStream())
                    {
                        using (StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("UTF-8")))
                        {
                            result = reader.ReadToEnd();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                if (req != null)
                {
                    req.Abort();
                }
            }
            return result;
        }
    }
}
